package P001;

import org.junit.Test;

import java.util.HashMap;

/**
 * @Author DJ同学
 * @Date 2021/2/7 12:50
 * @Version 1.0
 * @Name 两数之和
 * @Problem https://leetcode-cn.com/problems/two-sum/
 */
public class Solution2 {
    @Test
    public void test(){
        int[] nums ={2,7,11,15};
    //    int[] nums ={3,2,4};

        int[] indexs = twoSum(nums,9);
        System.out.println(indexs[0]);
        System.out.println(indexs[1]);
    }
    public int[] twoSum(int[] nums, int target) {
        int f=0;
        //key 数值 value下标
        HashMap<Integer,Integer> map = new HashMap<>();
        int i;
        for(i = 0;i<nums.length;i++){
            if(map.containsKey(target-nums[i])){
                f=1;
                break;
            }else{
                map.put(nums[i],i);
            }
        }
        return new int[]{map.get(target-nums[i]),i};
    }
}
